// pages/inviteRecords/inviteRecords.js
import { shareOptions } from "../../utils/access";
import api from "../../config/api";
import request from "../../utils/request";

const PAGE_SIZE = 15
var page = 1
var total = 0

Page({

  /**
   * 页面的初始数据
   */
  data: {
    records:[],
    isDone: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    page = 1
    total = 0
    this.inviteRecords()
  },

  failReason: function(res){
    var reason = res.currentTarget.dataset.reason
    if(reason){
      wx.showModal({
        title: '失败原因',
        content: reason,
        confirmColor: '#000',
        confirmText: '确定',
        showCancel:false
      })
    }
  
  },

  async inviteRecords(){
    const {data} = await request.get(api.inviteRecords,{
      page: page,
      size: PAGE_SIZE
    })

    total = data.pages
    let records = this.data.records
    records = records.concat(data.records)
    this.setData({
      records
    })

    wx.stopPullDownRefresh()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    page = 1
    this.setData({
      records:[],
      isDone: false,
    })
    this.inviteRecords()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(page < total){
      page++
      this.inviteRecords()
    }else{
      this.setData({
        isDone: true
      })
    }
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(e) {
    return shareOptions('default', e)
  },
  
})